// 首页加载完后，取消加载中状态
$(window).load(function () {
	$("#loading").fadeOut();
});

$(function () {

	// 首页tabs选项卡
	var index_tabs = $('#index_tabs').iTabs({
		fit: true,
	});

	// 修改密码窗口
	$('#pwdDialog').iDialog({
		buttons: [{
			text: '确定',
			iconCls: 'fa fa-save',
			btnCls: 'topjui-btn',
			handler: function () {
				if ($('#pwdDialog').form('validate')) {
					if ($("#password").val().length < 6) {
						$.iMessager.alert('警告', '密码长度不能小于6位', 'messager-warning');
					} else {
						var formData = $("#pwdDialog").serialize();
						$.ajax({
							url: path + '/updatePwd',
							type: 'post',
							cache: false,
							data: formData,
							beforeSend: function () {
								$.iMessager.progress({
									text: '正在操作...'
								});
							},
							success: function (data, response, status) {
								$.iMessager.progress('close');
								if (data.success) {
									$.iMessager.show({
										title: '提示',
										msg: '操作成功'
									});
									$("#pwdDialog").iDialog('close').form('reset');

								} else {
									$.iMessager.alert('操作失败！', data.msg, 'messager-error');
								}
							}
						});
					}
				}
			}
		}],
		onOpen: function () {
			//$(this).panel('refresh');
		}
	});

});

// Tab菜单操作
function tabMenuOprate(menu, type) {
	var allTabs = $('#index_tabs').iTabs('tabs');
	var allTabtitle = [];
	$.each(allTabs, function (i, n) {
		var opt = $(n).iPanel('options');
		if (opt.closable)
			allTabtitle.push(opt.title);
	});
	var curTabTitle = $(menu).data("tabTitle");
	var curTabIndex = $('#index_tabs').iTabs("getTabIndex", $('#index_tabs').iTabs("getTab", curTabTitle));
	switch (type) {
		case "1"://关闭当前
			if (curTabIndex > 0) {
				$('#index_tabs').iTabs("close", curTabTitle);
				return false;
				break;
			} else {
				$.iMessager.show({
					title: '操作提示',
					msg: '首页不允许关闭！'
				});
				break;
			}
		case "2"://全部关闭
			for (var i = 0; i < allTabtitle.length; i++) {
				$('#index_tabs').iTabs('close', allTabtitle[i]);
			}
			break;
		case "3"://除此之外全部关闭
			for (var i = 0; i < allTabtitle.length; i++) {
				if (curTabTitle != allTabtitle[i])
					$('#index_tabs').iTabs('close', allTabtitle[i]);
			}
			$('#index_tabs').iTabs('select', curTabTitle);
			break;
		case "4"://当前侧面右边
			for (var i = curTabIndex; i < allTabtitle.length; i++) {
				$('#index_tabs').iTabs('close', allTabtitle[i]);
			}
			$('#index_tabs').iTabs('select', curTabTitle);
			break;
		case "5": //当前侧面左边
			for (var i = 0; i < curTabIndex - 1; i++) {
				$('#index_tabs').iTabs('close', allTabtitle[i]);
			}
			$('#index_tabs').iTabs('select', curTabTitle);
			break;
		case "6": //刷新
			var refresh_tab = $('#index_tabs').iTabs('getSelected');
			var refresh_iframe = refresh_tab.find('iframe')[0];
			refresh_iframe.contentWindow.location.href = refresh_iframe.src;
			//$('#index_tabs').trigger(TOPJUI.eventType.initUI.base);
			break;
	}

}

/**
 * 更换页面风格
 * @param topjuiThemeName
 */
function changeTheme(themeName) {/* 更换主题 */
	var $dynamicTheme = $('#dynamicTheme');
	var themeHref = $dynamicTheme.attr('href');
	var themeHrefNew = themeHref.substring(0, themeHref.indexOf('themes')) + 'themes/default/topjui.' + themeName + '.css';
	// 更换主页面风格
	$dynamicTheme.attr('href', themeHrefNew);

	// 更换iframe页面风格
	var $iframe = $('iframe');
	if ($iframe.length > 0) {
		for (var i = 1; i < $iframe.length; i++) {
			var ifr = $iframe[i];
			var $iframeDynamicTheme = $(ifr).contents().find('#dynamicTheme');
			var iframeThemeHref = $iframeDynamicTheme.attr('href');
			var iframeThemeHrefNew = iframeThemeHref.substring(0, iframeThemeHref.indexOf('themes')) + 'themes/default/topjui.' + themeName + '.css';
			$iframeDynamicTheme.attr('href', iframeThemeHrefNew);
		}
	}

	$.cookie('topjuiThemeName', themeName, {
		expires: 7,
		path: '/'
	});
};
if ($.cookie('topjuiThemeName')) {
	changeTheme($.cookie('topjuiThemeName'));
}

// 退出系统
function logout() {
	$.iMessager.confirm('提示', '确定要退出吗?', function (r) {
		if (r) {
			$.iMessager.progress({
				text: '正在退出中...'
			});
			//console.info(path);
			window.location.href = path + '/logout';
		}
	});
}

//打开Tab窗口
function addTab(params) {
	var url = params.name;
	var iframe = '<iframe src="' + url + '" scrolling="auto" frameborder="0" style="width:100%;height:100%;"></iframe>';
	var t = $('#index_tabs');
	var $selectedTab = t.iTabs('getSelected');
	var selectedTabOpts = $selectedTab.iPanel('options');
	var opts = {
		id: getRandomNumByDef(),
		refererTab: {},
		title: params.text,
		closable: typeof(params.closable) != "undefined" ? params.closable : true,
		iconCls: params.iconCls ? params.iconCls : 'fa fa-file-text-o',
		content: iframe,
		//href: params.url,
		border: params.border || true,
		fit: true
		//cls: 'leftBottomBorder'
	};

	t.tabs({
		onSelect: function (title) {
			if (title == '历史轨迹') {
				var ligjTab = $('#index_tabs').tabs('getTab', '历史轨迹');
				if (ligjTab && ligjTab.find('iframe').length > 0) {
					var iframeWindow1 = ligjTab.find('iframe')[0];
					if (iframeWindow1 && iframeWindow1.contentWindow.loadEnclosures) {
						iframeWindow1.contentWindow.loadEnclosures();
					}
				}
			}
			if (title == '系统首页') {
				var iframeWindow = $("#mapFrame")[0];
				if (iframeWindow) {
					iframeWindow.contentWindow.loadEnclosures();
				}
			}
		}
	});

	if (t.iTabs('exists', opts.title)) {
		t.iTabs('select', opts.title);
		if (opts.title == '历史轨迹') {
			var selectTab = $('#index_tabs').tabs('getSelected');
			var node = DeviceTree.getSelectNode();
			var url = path + "/map/hisroute?dvcId=" + node.id.substring(1, node.id.length) + "&dvcSim=" + node.text;
			var iframe = '<iframe src="' + url + '" scrolling="auto" frameborder="0" style="width:100%;height:100%;"></iframe>';
			$('#index_tabs').tabs('update', {
				tab: selectTab,
				options: {
					content: iframe
				}
			})
			selectTab.panel('refresh');
		}
	} else {
		var lastMenuClickTime = $.cookie("menuClickTime");
		var nowTime = new Date().getTime();
		if ((nowTime - lastMenuClickTime) >= 500) {
			$.cookie("menuClickTime", new Date().getTime());
			t.iTabs('add', opts);
		} else {
			$.iMessager.show({
				title: '温馨提示',
				msg: '操作过快，请稍后重试！'
			});
		}
	}


}

function addParentTab(options) {
	var src, title;
	src = options.href;
	title = options.title;

	var iframe = '<iframe src="' + src + '" frameborder="0" style="border:0;width:100%;height:100%;"></iframe>';
	parent.$('#index_tabs').iTabs("add", {
		title: title,
		content: iframe,
		closable: true,
		iconCls: 'fa fa-th',
		border: true
	});
}

function modifyPwd() {
	$("#pwdDialog").iDialog('open');
};

//刷新当前标签Tabs
function RefreshTab(currentTab) {
	var url = $(currentTab.panel('options')).attr('href');
	$('#index_tabs').tabs('update', {
		tab: currentTab,
		options: {
			href: url
		}
	});
	currentTab.panel('refresh');
}